var win_width = $(window);

//全局函数
$.fn.extend({
    scrollFun: function (fun, options) {
        if (typeof fun === "function") {
            var defaults = {
                top: 30,
                loop: false,
                skip_invisible: true,
                is_scroll: false
            };
            $.extend(defaults, options);
            $(this).each(function () {
                var $self = $(this),
                    fun_open = true,
                    windowDistanceFun = function () {// 窗口距离触发回调
                        if (fun_open) {
                            var this_t = $self.offset().top,
                                scroll_t = $(window).scrollTop(),
                                this_scroll_t = this_t - scroll_t - $(window).height(),
                                this_scroll_b = this_t + $self.outerHeight() - scroll_t,
                                visible = defaults.skip_invisible ? $self.is(":visible") : true;
                            if (this_scroll_t < defaults.top && this_scroll_b > 0 && visible) {
                                if (!defaults.loop) fun_open = false;
                                fun($self);
                            }
                        }
                    };
                windowDistanceFun();
                // 滚动时窗口距离触发回调
                if (defaults.is_scroll) {
                    $(window).scroll(function () {
                        if (fun_open) windowDistanceFun();
                    })
                }
            });
        }
    },
    /**
     * appearDiy 手动appear动画
     * @param  {Boolean} is_reset 是否重置动画
     */
    appearDiy: function (is_reset) {
        $(this).each(function () {
            var $self = $(this),
                animation = 'animation-' + $(this).data('animate');
            if (is_reset) {
                // 重置动画
                $(this).removeClass(animation).removeClass('appear-no-repeat').addClass('invisible');
            } else {
                // 执行动画
                $(this).addClass(animation).addClass('appear-no-repeat');
                setTimeout(function () {
                    $self.removeClass('invisible');
                }, 0)
            }
        });
    },
});

// UI执行
function metui(obj) {
    this.obj = obj;
    var keys = [];
    var values = [];
    for (var key in obj) {
        keys.push(key);
        values.push(obj[key]);
        if (typeof(obj[key]) == 'string') {
            if (keys == 'name') {
                METUI[obj[key]] = $('.' + obj[key]);
            }
        } else if (typeof(obj[key]) == 'function') {
            this.obj[key]();
        }
    }
    return this;
}

window.METUI = METUI_FUN = new Array();


// 图片延迟加载
if (typeof $.fn.lazyload == 'function') {
    var $original = $('[data-original]');
    if ($original.length) $original.lazyload({placeholder: met_lazyloadbg});
}

(function () {
    $(window).resize(function () {
        window_hg = $(window).width();
        if ((window_hg > 767 && win_width < 767) || (window_hg < 767 && win_width > 767)) {
            clearTimeout(timer);
            timer = window.setTimeout(function () {
                window.location.reload();
            }, 300);
        }
    });

    //地图
    $('#openMask').click(function () {
        $("body").animate({'margin-left': '-320px'}, 200);
        $("#mask").show();
        $("#sitemap").animate({'right': 0}, 200);
    })
    $('#mask').click(function () {
        $("body").stop().animate({'margin-left': '0'}, 200);
        $("#sitemap").animate({'right': '-378px'}, 200);
        setTimeout(function () {
            $("#mask").hide();
        }, 200)
    })

    //侧边栏固定
    if ($('.leftSidebar')) {
        $('.leftSidebar').theiaStickySidebar({
            additionalMarginTop: 30
        });
    }
    if ($(".fullscreen")) {
        $(".fullscreen").height($(window).height())
    }
}());